package org.come.socket;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tool.ModerateTask.AllTask;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.come.MountShouHu.AllMountShouhu;
import org.come.bean.*;
import org.come.entity.RoleSummoning;
import org.come.login.LoginJpanel;
import org.come.model.*;
import org.come.until.GsonUtil;
import org.come.until.NewAESUtil;
import org.come.until.UserMessUntil;

@SuppressWarnings("unused")
public class DownLoadTxt {
  private static DownLoadTxt downLoadTxt;

  private String PATH;

  public static String ip;

  public static int port;

  public static DownLoadTxt getDownLoadTxt() {
    if (downLoadTxt == null) {
        downLoadTxt = new DownLoadTxt();
    }
    return downLoadTxt;
  }

  public DownLoadTxt() {
    String web = "GameServer";
//      String web = "/";
    StringBuffer buffer = new StringBuffer();
    buffer.append("http://");
//  buffer.append("103.239.247.203");//缘定三生
//  buffer.append("114.132.245.28");
//  buffer.append("81.68.131.90");//情义
//  buffer.append("150.158.184.183");//永恒
//  buffer.append("43.248.190.237");//君子意高远
//  buffer.append("101.43.12.117");//十里桃花
//  buffer.append("111.180.196.95");//剑浊沧海
//  buffer.append("203.135.101.232");//尘缘未了
//    buffer.append("43.139.222.95");//倾城-雷音寺
    buffer.append(ip);
    buffer.append(":");
      buffer.append(port);
    buffer.append("/");
    buffer.append(web);
    buffer.append("/GetTXT/");
    this.PATH = buffer.toString();
  }

  public void initMes(String filename) {
    deviceClassForMes(filename.split("\\.")[0], GetTxt(filename));
  }

  public void deviceClassForMes(String fileName, String MesStr) {
      switch (fileName) {



      case "itemExchange":// 召唤兽合成表
          allItemExchange allItemExchange = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, allItemExchange.class);
          UserMessUntil.setAllItemExchange((allItemExchange)GsonUtil.getGsonUtil().getgson().fromJson(MesStr, allItemExchange.class));
          UserMessUntil.setAllItemExchange(allItemExchange);
          break;

      case "goodsExchange":// 召唤兽合成表
          allGoodsExchange allGoodsExchange = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, allGoodsExchange.class);
          UserMessUntil.setAllGoodsExchange((allGoodsExchange)GsonUtil.getGsonUtil().getgson().fromJson(MesStr, allGoodsExchange.class));
          UserMessUntil.setAllGoodsExchange(allGoodsExchange);
          break;


          case "vipActive":
              AllActive allVipActive = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllActive.class);
              UserMessUntil.setAllVipActive(allVipActive);
              break;
      case "map":// 地图信息
          AllMapBean allmap = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllMapBean.class);
          UserMessUntil.setAllmapbean(allmap);
          break;
      case "npcshop":// npc商店信息
          NpcShopBean npcshop = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, NpcShopBean.class);
          UserMessUntil.setNpcshop(npcshop);
          break;
      case "exp":// 召唤兽和人物升级经验信息
          RoleExpBean petAndPlayerExp = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, RoleExpBean.class);
          UserMessUntil.setPetAndPlayerExp(petAndPlayerExp);
          break;
      case "petexp":// 召唤兽和人物升级经验信息
          PetExpBean petExp = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, PetExpBean.class);
          UserMessUntil.setPetExp(petExp);
          break;
      case "eshop":// 商城信息
          EshopBean eshopItem = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, EshopBean.class);
          UserMessUntil.setEshops(eshopItem.getEshops());
          break;
      case "skill":// 技能表
          SkillBean skillBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, SkillBean.class);
          UserMessUntil.setSkillBean(skillBean);
          break;
      case "babyresult":// 孩子结局
          AllBabyResult allBabyResult = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllBabyResult.class);
          UserMessUntil.setAllBabyResult(allBabyResult);
          break;
      case "goods":// 物品表
          GoodsBean goodsBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, GoodsBean.class);
          UserMessUntil.setGoodsBean(goodsBean);
          break;
      case "robots"://
          RobotsBean robotsBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, RobotsBean.class);
          UserMessUntil.setRobotBean(robotsBean);
          break;
      case "npc":
          AllNpcBean allNpcBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllNpcBean.class);
          UserMessUntil.setAllNpcBean(allNpcBean);
          break;
      case "door":// 传送
          AllDoorBean allDoorBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllDoorBean.class);
          UserMessUntil.setAllDoorBean(allDoorBean);
          break;
      case "task":// 任务
          AllTask allTask = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllTask.class);
          UserMessUntil.setAllTask(allTask);
          break;
      case "bbuy":// 回收表
          AllBbuy allBbuy = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllBbuy.class);
          UserMessUntil.setAllBbuy(allBbuy);
          break;
      case "talent":// 天资
          AllTalent allTalent = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllTalent.class);
          UserMessUntil.setAllTalent(allTalent);
          break;
      case "color":// 颜色方案
          AllColorScheme allColorScheme = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllColorScheme.class);
          UserMessUntil.setAllColorScheme(allColorScheme);
          break;
      case "tx":// 特效配置
          AllTx allTx = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllTx.class);
          UserMessUntil.setAllTx(allTx);
          break;
      case "suit":// 套装表
          AllSuit allSuit = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllSuit.class);
          UserMessUntil.setAllSuit(allSuit);
          break;
      case "guide":// 引导
          AllGuide allGuide = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllGuide.class);
          UserMessUntil.setAllGuide(allGuide);
          break;
      case "tyc":// 天演策表
          @SuppressWarnings("unchecked") HashMap<String, String> allTYC = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, HashMap.class);
          UserMessUntil.setAllTYC(allTYC);
          break;
      case "acard":// 七十二变表
          AllACard allACard = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllACard.class);
          UserMessUntil.setAllACard(allACard);
          break;
      case "title":// 称谓
          if (MesStr != null) {
              AllTitleBean allTitleBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllTitleBean.class);
              UserMessUntil.setAllTitle(allTitleBean);
          } else {
              AllTitleBean allTitleBean = new AllTitleBean();
              HashMap<String, Title> hashMap = new HashMap<>();
              allTitleBean.setTitleMap(hashMap);
              UserMessUntil.setAllTitle(allTitleBean);
          }
          break;
      case "event":// 活动表
          AllEventModelBean allEvent = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllEventModelBean.class);
          UserMessUntil.setAllEvent(allEvent);
          break;
      case "pal":// 伙伴表
          AllPal allPal = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllPal.class);
          UserMessUntil.setAllPal(allPal);
          break;
      case "petExchange":// 召唤兽合成表
          allPetExchange allPetExchange = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, allPetExchange.class);
          UserMessUntil.setAllPetExchange(allPetExchange);
          break;
      case "active":
          AllActive allActive = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllActive.class);
          UserMessUntil.setAllActive(allActive);
          break;
      case "achieve"://竞技称号表
          AllAchieve allAchieve = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllAchieve.class);
          UserMessUntil.setAllAchieve(allAchieve);
          break;
      case "lh"://炼化表
          AllLianHua allLianHua = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllLianHua.class);
          UserMessUntil.setAllLianHua(allLianHua);//.setAllAchieve(allAchieve);
          break;
      case "meridians"://经脉表
          AllMeridians all = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllMeridians.class);
          UserMessUntil.setAllMeridians(all);//(all);//.setAllAchieve(allAchieve);
          break;
      case "qiandao":// 签到
          QianDaoBean qianDaoBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, QianDaoBean.class);
          UserMessUntil.setQianDaoList(new ArrayList<>(qianDaoBean.getAllQianDao().values()));
          break;
      case "configure":// 配置
              ConfigureBean configureBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, ConfigureBean.class);
              UserMessUntil.setConfigureBean(configureBean);
           break;
      case "GMshopItem":// GM特权
    	  GMshopItemBean gMshopItemBean = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, GMshopItemBean.class);
          UserMessUntil.setAllGMshopItem(gMshopItemBean);
          break;
          case "shssx":
              AllMountShouhu allMountShouhu= GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllMountShouhu.class);
              UserMessUntil.setAllMountShouhu(allMountShouhu);
              break;
          case "Hallucination":
              AllHallucination allHallucination = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllHallucination.class);
              UserMessUntil.setAllHallucination(allHallucination);
              break;
          case "pet":
              AllPet RoleSummoning = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllPet.class);
              UserMessUntil.setAllPet(RoleSummoning);
              break;
      case "autoactive":
          AutoActiveBaseBean allautobase = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AutoActiveBaseBean.class);
          UserMessUntil.setAlltutoactive(allautobase);
          break;
          case "achievement":// 功绩千秋
              AllAchievement allAchievement = GsonUtil.getGsonUtil().getgson().fromJson(MesStr, AllAchievement.class);
              UserMessUntil.setAllAchievement(allAchievement);
              break;
      }

  }
  // 获取文本
  public String GetTxt(String path) {
      byte[] v = null;
      try {
          v = downloadtxt(new URL(PATH + path + "?" + System.currentTimeMillis()));
//          if (v.length > 10) {
//              byte a = v[v.length - 4];
//              v[v.length - 4] = v[4];
//              v[4] = a;
//          }
//          v = NewAESUtil.Dncode.doFinal(v);
//          v = YASUO.unGZip(v);
          String message = new String(v, NewAESUtil.UTF_8);
          String aesjdkDncode_utf8 = NewAESUtil.AESJDKDncode(message);
          return aesjdkDncode_utf8;
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return null;
  }

  /**
   * 下载txt
   */
  public byte[] downloadtxt(URL url) {
      try {
          long x = System.currentTimeMillis();
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          InputStream bis = url.openStream();
          byte[] buf = new byte[1024];
          int count = 0;
          while ((count = bis.read(buf)) != -1) {
              bos.write(buf, 0, count);
          }
          bos.close();
          bis.close();
          x = System.currentTimeMillis() - x;
          if (x > 1000) {
              System.err.println("资源下载完毕:耗时" + x);
          }
          return bos.toByteArray();
      } catch (IOException e) {
          System.err.println("下载资源失败 error=" + e.getMessage() + ":" + url.getPath());
          e.printStackTrace();
      }
      return null;
  }

  public String GetServerTxt(String path) {
      @SuppressWarnings("unused")
	Object var2 = null;

      try {
          byte[] v = this.downloadtxt(new URL(this.PATH + path + "?" + System.currentTimeMillis()));
          String message = new String(v, NewAESUtil.UTF_8);
          return message;
      } catch (Exception var4) {
          var4.printStackTrace();
          return null;
      }
  }
    public static String GetServerTxtGG(String path) {
        Object var2 = null;

        try {
            String p;

            String web = "GameServer";

            StringBuffer buffer = new StringBuffer();
            buffer.append("http://");
            // ip
            buffer.append(LoginJpanel.Rip);//热更新IP
            buffer.append(":");
            // tomcat端口
            buffer.append(LoginJpanel.Rport);//热更新端口
            buffer.append("/");
            // 项目名
            buffer.append(web).append("/");
            // 文件夹
            // buffer.append("/GameServer/");
            p = buffer.toString();
            byte[] v = downloadtxtGG(new URL(p + path + "?" + System.currentTimeMillis()));
            String message = new String(v, NewAESUtil.UTF_8);
            return message;
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }
    public static byte[] downloadtxtGG(URL url) {
        try {
            long x = System.currentTimeMillis();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream bis = url.openStream();
            byte[] buf = new byte[1024];
            int count = 0;
            while ((count = bis.read(buf)) != -1) {
                bos.write(buf, 0, count);
            }
            bos.close();
            bis.close();
            x = System.currentTimeMillis() - x;
            if (x > 1000) {
                System.err.println("资源下载完毕:耗时" + x);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            System.err.println("下载资源失败 error=" + e.getMessage() + ":" + url.getPath());
            e.printStackTrace();
        }
        return null;
    }
}
